package com.tsd.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.tsd.core.cache.CacheOperatorApi;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.SystemSetting;
import com.tsd.system.entity.SysSettingExt;
import com.tsd.system.service.SysSettingService;
import com.tsd.system.service.SystemSettingService;
import com.tsd.system.vo.GlobalSetting;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.tsd.system.SystemSetting.SETTING_CODE;
import static com.tsd.system.vo.GlobalSetting.DISPLAY_NAME;

/**
 * @ClassName: SystemSettingServiceImpl
 * @Description: 系统管理配置实现
 * @Author: Hillpool
 * @Date: 2022/02/06 20:43
 * @Version: 1.0
 */
@Service
public class SystemSettingServiceImpl extends BaseServiceImpl implements SystemSettingService {

    @Resource
    private SysSettingService sysSettingService;
    @Resource
    private CacheOperatorApi<GlobalSetting> globalSettingCacheOperatorApi;

    private static final Map<String, SystemSetting> SYSTEM_SETTING_MAP;

    static {
        SYSTEM_SETTING_MAP = new ConcurrentHashMap<>();
    }

    @Override
    public SystemSetting getSystemSetting() throws Exception {
        String accountSetSid = DataSourceContextHolder.getDataSourceType();
        SystemSetting systemSetting = SYSTEM_SETTING_MAP.get(accountSetSid);
        if (systemSetting == null) {
            SysSettingExt query = new SysSettingExt();
            query.setCode(SETTING_CODE);
            query.setDr(0);
            SysSettingExt sysSetting = sysSettingService.getSysSettingByCodeOwnerSid(query);
            if (sysSetting != null) {
                systemSetting = JSON.parseObject(sysSetting.getJson(), SystemSetting.class);
                SYSTEM_SETTING_MAP.put(accountSetSid, systemSetting);
            }
        }
        return systemSetting;
    }

    @Override
    public void clearSystemSettingCache() {
        String accountSetSid = DataSourceContextHolder.getDataSourceType();
        SYSTEM_SETTING_MAP.remove(accountSetSid);
    }

    @Override
    public GlobalSetting getGlobalSetting() throws Exception {
        String accountSetSid = DataSourceContextHolder.getDataSourceType();
        GlobalSetting globalSetting = globalSettingCacheOperatorApi.get(accountSetSid);
        if (globalSetting == null) {
            SysSettingExt query = new SysSettingExt();
            query.setCode(GlobalSetting.SETTING_CODE_GLOBAL_SETTING);
            query.setDr(0);
            SysSettingExt sysSetting = sysSettingService.getSysSettingByCodeOwnerSid(query);
            if (sysSetting != null) {
                globalSetting = JSON.parseObject(sysSetting.getJson(), GlobalSetting.class);
            } else {
                sysSetting = new SysSettingExt();
                globalSetting = new GlobalSetting();
                globalSetting.setRelated_party_display(DISPLAY_NAME);
                sysSetting.setCode(GlobalSetting.SETTING_CODE_GLOBAL_SETTING);
                sysSetting.setJson(JSON.toJSONString(globalSetting));
                sysSettingService.saveSysSetting(sysSetting, null);
            }
            globalSettingCacheOperatorApi.put(accountSetSid, globalSetting);
        }
        return globalSetting;
    }

    @Override
    public void clearGlobalSettingCache() {
        String accountSetSid = DataSourceContextHolder.getDataSourceType();
        globalSettingCacheOperatorApi.remove(accountSetSid);
    }
}
